/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.TourBookingBean;
import java.io.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerBookTour", urlPatterns = {"/ControllerBookTour"})
public class ControllerBookTour extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Date date=new Date();
        
        SimpleDateFormat simpleDate=new SimpleDateFormat("MM/dd/yyyy");
        String strdate=simpleDate.format(date);
        HttpSession session=request.getSession(true);
        
        TourBookingBean tbook=new TourBookingBean();
        
        String idtour=request.getParameter("idtourr");
        String username=(String) session.getAttribute("User");
        String nametour=request.getParameter("nametourr");
        
        String priceAdu=request.getParameter("app");
        String priceChil=request.getParameter("cpp");
        
        float pAdul=Float.parseFloat(priceAdu);
        float pchil=Float.parseFloat(priceChil);
        
        String numberOfAdu=request.getParameter("opadultt");
        String numberOfchild=request.getParameter("opchildd");
        String startDate=request.getParameter("startdatee");
        String endDate=request.getParameter("enddatee");
        String cash=request.getParameter("methodPayy");
        
        int numOfchild=Integer.parseInt(numberOfchild);
        int numOfAdu=Integer.parseInt(numberOfAdu);
        
        
        
        float mchil=numOfchild*pchil;
        float ma=numOfAdu*pAdul;
        
        int sumGuest=numOfchild + numOfAdu;
        float sumMoney=mchil+ma;
        
        StringTokenizer t = new StringTokenizer(startDate, "-");
        String datee = t.nextToken();
        String month = t.nextToken();
        String year = t.nextToken();
        String dateStart=month+"/"+datee+"/"+year;
        
        StringTokenizer e = new StringTokenizer(endDate, "-");
        String d = e.nextToken();
        
        String m = e.nextToken();
        String y = e.nextToken();
        String dateEnd=m +"/"+d +"/"+y;
        
        String touCancel="False";
        
        try {
            
            boolean kq=tbook.InsertTourBooking(idtour,username,nametour,numOfchild,numOfAdu,sumGuest,sumMoney,dateStart,dateEnd,strdate,touCancel,cash);
            if(kq==true) {
                out.println("<script>alert('You finish booking tour!');window.location='index.jsp?page=BillBookTours';</script>");
                
                
            } else {
                
                out.println("<script>alert('Book tour fail, Pleasew try agian!');window.location='index.jsp?page=listTour';</script>");
                
                
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
